package com.ezops.poc.util;

import static org.dozer.loader.api.TypeMappingOptions.mapNull;

import java.util.Iterator;
import java.util.Map;

import org.dozer.DozerBeanMapper;
import org.dozer.loader.api.BeanMappingBuilder;
import org.dozer.loader.api.TypeMappingBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ezops.poc.bo.ClientBondFileModel;
import com.ezops.poc.bo.ClientEquityFileModel;
import com.ezops.poc.bo.EzopsBondModel;
import com.ezops.poc.bo.EzopsEquityModel;
import com.ezops.poc.bo.EzopsOTEquityModel;
import com.ezops.poc.bo.ot.ClientOTEquityModel;
import com.ezops.poc.dao.FieldMappingDAO;

/**
 * Test class to provide mapping between ezops field to client field.
 * 
 * @author vgaurav
 * 
 */
@Component(value="fieldMapper")
public class ClientEzopsMapping {

	@Autowired
	FieldMappingDAO dao;

	public Map<String, String> bondMapping;
	public Map<String, String> equityMapping;
	public Map<String, String> equityOtMapping;
	private DozerBeanMapper mapper = new DozerBeanMapper();
	private DozerBeanMapper equityMapper = new DozerBeanMapper();
	private boolean isMappingLoaded = false;

	public void buildMapping() {

		if (isMappingLoaded) {
			return;
		}

		loadMappings();

	}

	private void loadMappings() {
		isMappingLoaded = true;
		
		bondMapping = dao.getMapping(1, 1);
		equityMapping = dao.getMapping(1, 2);
		equityOtMapping = dao.getMapping(1, 3);
		final Iterator<String> itr = bondMapping.keySet().iterator();

		BeanMappingBuilder builder = new BeanMappingBuilder() {
			protected void configure() {
				TypeMappingBuilder builder = mapping(EzopsBondModel.class,
						ClientBondFileModel.class, mapNull());
				while (itr.hasNext()) {
					String key = itr.next();
					builder.fields(key, bondMapping.get(key));
				}

				TypeMappingBuilder anotherBuilder = mapping(
						EzopsEquityModel.class, ClientEquityFileModel.class,
						mapNull());
				while (itr.hasNext()) {
					String key = itr.next();
					anotherBuilder.fields(key, equityMapping.get(key));
				}
			}

		};

		mapper.addMapping(builder);
		
		//Map equity fields
		equityOtMapping = dao.getMapping(1, 3);
		
		final Iterator<String> itrerator = equityOtMapping.keySet().iterator();
		BeanMappingBuilder equityBuilder = new BeanMappingBuilder() {
			protected void configure() {
				TypeMappingBuilder builder = mapping(EzopsOTEquityModel.class,
						ClientOTEquityModel.class, mapNull());
				while (itrerator.hasNext()) {
					String equitykey = itrerator.next();
					String pojoField = equityOtMapping.get(equitykey);
					 pojoField = Character.toLowerCase(equitykey.charAt(0))
							+ equitykey.substring(1, equitykey.length());
					 equitykey = pojoField.replace(".", "").replace(" ", "");
					builder.fields(equitykey, pojoField);
				}

			}

		};

		equityMapper.addMapping(equityBuilder);
		
		
	}

	public void convert(Object src, Object dstn) {
		buildMapping();
		mapper.map(src, dstn);
	}
	
	public void convert(ClientOTEquityModel clientModel, EzopsOTEquityModel ezopsModel){
		buildMapping();
		equityMapper.map(clientModel, ezopsModel);
	}
	
	public void convert(EzopsOTEquityModel ezopsModel, ClientOTEquityModel clientModel){
		buildMapping();
		equityMapper.map(ezopsModel, clientModel);
	}

}
